layui.use(['jquery', 'element', 'table', 'form', 'laypage'], function () {
    var $ = layui.jquery,
        form = layui.form,
        table = layui.table,
        laypage = layui.laypage;
});


// 添加弹窗
function add(t){
    layer.open({
      type: 2,
      title: $(t).attr('data-title'),
      shade: 0.3,
      area: [$(t).attr('data-width'), $(t).attr('data-height')],
      content: $(t).attr('data-url')
    });
}

 // ajax提交保存或者更新
function save(t){
    var url = $(t).attr('data-url');
    $.post(url, $('form').serialize(), function(res){
        if(res.code == 0){
            if(res.data.url){   //如果有返回跳转地址的话优先跳转

                layer.msg(res.message,{icon:1});
                setTimeout(function(){
                    window.location.href=res.data.url;
                },2000);

            } else{     //没有跳转地址的话关闭子窗口，刷新父窗口

                layer.msg(res.message,{icon:1});
                setTimeout(function(){
                    window.parent.location.reload();
                },1000);

            }
        }else{
            layer.alert(res.message,{icon:2});
        }
    },'json');
}

// 编辑弹窗
function edit(t){
    var url = $(t).attr('data-url');
    var id = $(t).attr('data-id');
    layer.open({
      type: 2,
      title: $(t).attr('data-title'),
      shade: 0.3,
      area: [$(t).attr('data-width'), $(t).attr('data-height')],
      content: url+'?id='+id
    });
}

// 删除弹窗
function del(d){
  console.log(11111)
    var url = $(d).attr('data-url');
    var id = $(d).attr('data-id');
    layer.confirm('确定要删除吗？', {
      btn: ['确定','取消'],
      title: '操作提示',
      anim: 6
    }, function(){
      $.post(url,{'id':id},function(res){
        if(res.code == 0){
            layer.msg(res.message,{icon:1});
            setTimeout(function(){
                window.location.reload();
            },1000);
        }else{
            layer.msg(res.message,{icon:2});
        }
      },'json');
    });
}

// 请空，批量删除等
function go_action(d){
    var url = $(d).attr('data-url');
    var desc = $(d).attr('data-desc');
    layer.confirm(desc, {
      btn: ['确定','取消'],
      title: '操作提示',
      anim: 6
    }, function(){
      $.post(url,{},function(res){
        if(res.code == 0){
            layer.msg(res.message,{icon:1});
            setTimeout(function(){
                window.location.reload();
            },1000);
        }else{
            layer.msg(res.message,{icon:2});
        }
      },'json');
    });
}